package com.supreme.studyserver.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.studyserver.entity.Cpu;
import com.supreme.studyserver.entity.Thing;
import com.supreme.studyserver.mapper.CpuMapper;
import com.supreme.studyserver.mapper.ThingMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/secondary")
public class SecondaryController {

    @Resource
    private ThingMapper thingMapper;

    @Resource
    private CpuMapper cpuMapper;

    /**
     * insert
     * @param reqBody
     * @return response
     * http post :7230/api/secondary/insertSecondary title=title0002 summary="hello world" type=1 point=99.99
     */
    @PostMapping(value = "insertSecondary")
    public AjaxResp insertSecondary(@RequestBody Thing reqBody) {
        int insert = thingMapper.insert(reqBody);
        return AjaxResp.instance(insert);
    }

    @PutMapping(value = "updateSecondary")
    public AjaxResp updateSecondary(@RequestBody Thing reqBody) {
        LambdaUpdateWrapper<Thing> updateWrapper = Wrappers.lambdaUpdate(Thing.class)
                .eq(Thing::getSecondaryId, reqBody.getSecondaryId());
        int update = thingMapper.update(reqBody, updateWrapper);
        return AjaxResp.instance(update);
    }

    @GetMapping(value = "getSecondary")
    public AjaxResp getSecondary() {
        List<Thing> secondaryList = thingMapper.selectList(Wrappers.emptyWrapper());
        return AjaxResp.instance(secondaryList);
    }

    /**
     * insert
     * @param reqBody
     * @return response
     */
    @PostMapping(value = "insertCpu")
    public AjaxResp insertCpu(@RequestBody Cpu reqBody) {
        int insert = cpuMapper.insert(reqBody);
        return AjaxResp.instance(insert);
    }
}
